import { Component, OnInit } from '@angular/core';
import { ApiService } from './../../../../core/api/api.service';
import { FormBuilder, Validators } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd';
import { MessageService } from './../../../../core/service/message.service';
import { NzModalService } from 'ng-zorro-antd/modal';
@Component({
    templateUrl: './auditingList.html',
})
export class PrescriptionAuditingComponent implements OnInit {
    page = {pageNo: 1, pageSize: 10, total: 0};
    departmentList = [];
    departmentTypes = {};
    memo;
    departmentTypesList = [];
    medicineList = [];
    selectedRowData: any = {};
    isVisible = false;
    isLoading = false;
    editTable = false;
    statusType = {
        'HOSPITAL_AUDIT': '等待药师审核',
        'HOSPITAL_REFUSE': '审核未通过',
        'HOSPITAL_APPROVE': '选择取药药店',
        'PHARMACY_AUDIT': '药店药师审核',
        'PHARMACY_REFUSE': '药店审核未通过',
        'PHARMACY_APPROVE': '前往药店取药',
        'COMPLETE': '已取药',
    };
    validateForm;
    constructor(
        private apiService: ApiService,
        private fb: FormBuilder,
        private message: NzMessageService,
        private msg: MessageService,
        private modalService: NzModalService,
        ) {
    }
    ngOnInit() {
        this.msg.setTitle('处方审核');
        this.loadList();
        this.validateForm = this.fb.group({
            departmentName: [null, [Validators.required]],
            departmentTypeId: [null, [Validators.required]],
            memo: [null],
        });
    }
    loadList() {
        this.isLoading = true;
        this.apiService.post('处方审核列表(医院药师)', {...this.page}).then((res: any) => {
            this.departmentList = res.data.list;
            this.page.total = res.data.pages * 10;
            this.isLoading = false;
        });
    }
    changeCurrentPage(e) {
        this.page.pageNo = e;
        this.loadList();
    }
    approve() {
        if (!this.selectedRowData.id) {
            this.message.warning('请选择一条数据！');
            return;
        }
        if(this.selectedRowData.status !== 'HOSPITAL_AUDIT'){
            this.message.warning('当前处方已审核！');
            return;
        }
        this.modalService.confirm({
            nzTitle: '系统提示',
            nzContent: '确认当前处方审核入库？',
            nzOnOk: () => {
                this.apiService.post('审核处方(医院药师)', {id: this.selectedRowData.id, status: 'HOSPITAL_APPROVE', memo: ''}).then((res: any) => {
                    this.message.success('处方审核通过！');
                    this.loadList();
                });
            }
        });
    }
    reject() {
        if (!this.selectedRowData.id) {
            this.message.warning('请选择一条数据！');
            return;
        }
        if(this.selectedRowData.status !== 'HOSPITAL_AUDIT'){
            this.message.warning('当前处方已审核！');
            return;
        }
        this.isVisible = true;
    }
    rejecting(){
        if (this.memo) {
            this.apiService.post('审核处方(医院药师)', {id: this.selectedRowData.id, status: 'HOSPITAL_REFUSE', memo: this.memo}).then((res: any) => {
                this.isVisible = false;
                this.memo = '';
                this.message.success('处方审核驳回！');
                this.loadList();
            });
        }else{
            this.message.warning('请填写驳回原因!');
        }
    }
    selectedRow(data){
        this.selectedRowData = data;
        this.isLoading = true;
        this.apiService.post('处方审核详情(医院药师)', {id: data.id}).then((res: any) => {
            this.selectedRowData = res.data.prescription;
            this.medicineList = res.data.medicineList;
            this.isLoading = false;
        });
    }
}
